import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith

import org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager

/**
* A simple example using the HTTP plugin that shows the retrieval of a single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author ${userName}
*/
@RunWith(GrinderRunner)
<#if options??>
	<#assign options = options?eval>
	<#assign method = options["method"]>
	<#assign headers = options["headers"]>
	<#if options["body"]??>
		<#assign body = options["body"]>
	<#else>
		<#assign params = options["params"]>
	</#if>
	<#assign cookies = options["cookies"]>
</#if>
class TestRunner {

	public static GTest test
	public static HTTPRequest request
	public static Map<String, String> headers = [:]
	<#if body??>
	public static String body = "${body?j_string?replace("$", "\\$")}"
	<#else>
	public static Map<String, Object> params = [:]
	</#if>
	public static List<Cookie> cookies = []

	@BeforeProcess
	public static void beforeProcess() {
		HTTPRequestControl.setConnectionTimeout(300000)
		test = new GTest(1, "${name}")
		request = new HTTPRequest()
		<#if headers?? && headers?size != 0>

		// Set header data
		<#list headers as header>
		headers.put("${header["name"]?j_string}", "${header["value"]?j_string?replace("$", "\\$")}")
		</#list>
		</#if>
		<#if params?? && params?size != 0>

		// Set param data
		<#list params as param>
		params.put("${param["name"]?j_string}", "${param["value"]?j_string?replace("$", "\\$")}")
		</#list>
		</#if>
		<#if cookies?? && cookies?size != 0>

		// Set cookie data
		<#list cookies as cookie>
		cookies.add(new Cookie("${cookie["name"]?j_string}", "${cookie["value"]?j_string?replace("$", "\\$")}", "${cookie["domain"]?j_string}", "${cookie["path"]?j_string}", Integer.MAX_VALUE))
		</#list>

		</#if>
		grinder.logger.info("before process.")
	}

	@BeforeThread
	public void beforeThread() {
		test.record(this, "test")
		grinder.statistics.delayReports = true
		grinder.logger.info("before thread.")
	}

	@Before
	public void before() {
		request.setHeaders(headers)
		CookieManager.addCookies(cookies)
		grinder.logger.info("before. init headers and cookies")
	}

	@Test
	public void test() {
		HTTPResponse response = request.${method?default("GET")}("${url}", <#if body??>body.getBytes()<#else>params</#if>)

		if (response.statusCode == 301 || response.statusCode == 302) {
			grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
		} else {
			assertThat(response.statusCode, is(200))
		}
	}
}
